package com.gpjcjl.ui;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.base.lib.JsonUtil;
import com.base.lib.Logs;
import com.gpjcjl.App;
import com.gpjcjl.database.DbHelper;
import com.gpjcjl.entity.Record;
import com.gpjcjl.entity.RecordDetail;
import com.gpjcjl.entity.Result;
import com.gpjcjl.http.HttpManager;
import com.gpjcjl.http.HttpService;
import com.gpjcjl.http.RxSchedulers;
import com.gpjcjl.http.SingleLiveEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.CompositeDisposable;

/**
 * @author yinbiao
 */
public class HistoryViewModel extends AndroidViewModel {

    private static final String TAG = "MainViewModel";

    CompositeDisposable disposable;

    public HistoryViewModel(@NonNull Application application) {
        super(application);
        disposable = new CompositeDisposable();
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        //页面销毁时调用
        Logs.d(TAG, "onCleared:");
        disposable.clear();
    }

    /**
     * 上传离线数据
     *
     * @return LiveData
     */
    @SuppressLint("HardwareIds")
    public LiveData<Result<String>> syncRecord(Record record) {
        MutableLiveData<Result<String>> result = new SingleLiveEvent<>();
        List<RecordDetail> list = DbHelper.getInstance().getDataBase().recordDetailDao().findByRecordId(record.getId());
        if (list == null || list.isEmpty()) {
            result.setValue(Result.error("请先添加记录详情"));
            return result;
        }
        //        List<Map<String, String>> maps = new ArrayList<>();
        //        for (RecordDetail detail : list) {
        //            Map<String, String> map = new HashMap<>();
        //            map.put("check_num", detail.getCheck_num());
        //            map.put("produce", detail.getProduce());
        //            map.put("produce_time", detail.getProduce_time());
        //            map.put("num", detail.getNum());
        //            map.put("around", detail.getAround());
        //            map.put("wall", detail.getWall());
        //            map.put("weight", detail.getWeight());
        //            map.put("bottle", detail.getBottle());
        //            map.put("water_gage", detail.getWater_gage());
        //            map.put("airtight", detail.getAirtight());
        //            map.put("epilog", detail.getEpilog());
        //            map.put("scrap", detail.getScrap());
        //            maps.add(map);
        //        }
        String recordJson = JsonUtil.toJson(record);
        //        String listJson = JsonUtil.toJson(maps);
        String listJson = JsonUtil.toJson(list);
        System.out.println("record=" + recordJson);
        System.out.println("record_detail=" + listJson);

        disposable.add(HttpManager.getInstance().getService(HttpService.class)
                .syncRecord(App.getInstance().getAndroidId(), recordJson, listJson)
                .compose(RxSchedulers.ioMain())
                .subscribe(bean -> {
                    result.setValue(bean);
                }, throwable -> {
                    result.setValue(Result.error(throwable.getMessage()));
                    Logs.e(TAG, throwable.toString() + "-----" + throwable.getMessage());
                }));
        return result;
    }

    @SuppressLint("HardwareIds")
    public LiveData<Result<List<Record>>> getRecordList(int page) {
        MutableLiveData<Result<List<Record>>> result = new SingleLiveEvent<>();
        disposable.add(HttpManager.getInstance().getService(HttpService.class)
                .getRecordList(App.getInstance().getAndroidId(), page, 20)
                .compose(RxSchedulers.ioMain())
                .subscribe(bean -> {
                    result.setValue(bean);
                }, throwable -> {
                    result.setValue(Result.error(throwable.getMessage()));
                    Logs.e(TAG, throwable.toString() + "-----" + throwable.getMessage());
                }));
        return result;
    }

    @SuppressLint("HardwareIds")
    public LiveData<Result<Record>> editRecord(Record record) {
        Map<String, String> params = new HashMap<>();
        params.put("report_num", record.getReport_num());
        params.put("rid", record.getId());
        params.put("water", record.getWater());
        params.put("ambient", record.getAmbient());
        params.put("check_time", record.getCheck_time());
        params.put("property", record.getProperty());
        MutableLiveData<Result<Record>> result = new SingleLiveEvent<>();
        disposable.add(HttpManager.getInstance().getService(HttpService.class)
                .editRecord(App.getInstance().getAndroidId(), params)
                .compose(RxSchedulers.ioMain())
                .subscribe(bean -> {
                    result.setValue(bean);
                }, throwable -> {
                    result.setValue(Result.error(throwable.getMessage()));
                    Logs.e(TAG, throwable.toString() + "-----" + throwable.getMessage());
                }));
        return result;
    }

    @SuppressLint("HardwareIds")
    public LiveData<Result<String>> delRecord(Record record) {
        MutableLiveData<Result<String>> result = new SingleLiveEvent<>();
        disposable.add(HttpManager.getInstance().getService(HttpService.class)
                .delRecord(App.getInstance().getAndroidId(), record.getId())
                .compose(RxSchedulers.ioMain())
                .subscribe(bean -> {
                    result.setValue(bean);
                }, throwable -> {
                    result.setValue(Result.error(throwable.getMessage()));
                    Logs.e(TAG, throwable.toString() + "-----" + throwable.getMessage());
                }));
        return result;
    }

}
